﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using PaymentHelper.PaymentFlow;
using PaymentHelper.Core.Extension;
using PaymentHelper.PaymentFlow.ModelTemplate;

namespace PaymentHelper.PaymentFlowTemplates.Default
{
    public class DefaultPaymentFlowTemplateProcessor:BasePaymentFlow
    {
        public DefaultPaymentFlowTemplateProcessor() : base() { }

        public override void GetOrderAddressInfo(FormCollection frmData)
        {
            if (OrderPayment.ShippingAddress == null)
                OrderPayment.ShippingAddress = new Core.RelatedObject.Address();
            if (OrderPayment.BillingAddress == null)
                OrderPayment.BillingAddress = new Core.RelatedObject.Address();
            
            OrderPayment.BillingAddress.Address1 = frmData["Address1"];
            OrderPayment.BillingAddress.Address2 = frmData["Address2"];
            OrderPayment.BillingAddress.City = frmData["City"];
            OrderPayment.BillingAddress.Country = frmData["Country"];
            OrderPayment.BillingAddress.Email = frmData["Email"];
            OrderPayment.BillingAddress.FirstName = frmData["FirstName"];
            OrderPayment.BillingAddress.LastName = frmData["LastName"];
            OrderPayment.BillingAddress.PhoneNumber = frmData["PhoneNumber"];

            OrderPayment.ShippingAddress.Address1 = frmData["ship-address1"];
            OrderPayment.ShippingAddress.Address1 = frmData["ship-address2"];
            OrderPayment.ShippingAddress.City = frmData["ship-city"];
            OrderPayment.ShippingAddress.Country = frmData["ship-country"];
            OrderPayment.ShippingAddress.Email = frmData["ship-email"];
            OrderPayment.ShippingAddress.FirstName = frmData["ship-first-name"];
            OrderPayment.ShippingAddress.LastName = frmData["ship-last-name"];
            OrderPayment.ShippingAddress.PhoneNumber = frmData["ship-fone"];
            OrderPayment.ShippingAddress.ZipPostalCode = frmData["ship-zipcode"];
        }

        public override void RegisterRoute(RouteCollection routes)
        {
            routes.MapRoute(
                "DefaultTemplate.OrderInfo",
                "ProcessPayment/OrderInfo",
                new { controller = "DefaultTemplate", action = "OrderInfo" },
                new[] { "PaymentHelper.PaymentFlowTemplates.Default.Controllers" }
                );

            routes.MapRoute(
                "DefaultTemplate.Methods",
                "ProcessPayment/Methods",
                new { controller = "DefaultTemplate", action = "Methods" },
                new[] { "PaymentHelper.PaymentFlowTemplates.Default.Controllers" }
                );

            routes.MapRoute(
                "DefaultTemplate.PaymentInfo",
                "ProcessPayment/PaymentInfo",
                new { controller = "DefaultTemplate", action = "PaymentInfo" },
                new[] { "PaymentHelper.PaymentFlowTemplates.Default.Controllers" }
                );

            routes.MapRoute(
                "DefaultTemplate.Confirm",
                "ProcessPayment/Confirm",
                new { controller = "DefaultTemplate", action = "Confirm" },
                new[] { "PaymentHelper.PaymentFlowTemplates.Default.Controllers" }
                );

            routes.MapRoute(
                "DefaultTemplate.Cancle",
                "ProcessPayment/Cancle",
                new { controller = "DefaultTemplate", action = "Cancle" },
                new[] { "PaymentHelper.PaymentFlowTemplates.Default.Controllers" }
                );

            routes.MapRoute(
                "DefaultTemplate.Complete",
                "ProcessPayment/Complete",
                new { controller = "DefaultTemplate", action = "Complete" },
                new[] { "PaymentHelper.PaymentFlowTemplates.Default.Controllers" }
                );

        }

        public override RouteModel GetFirstStepRoute()
        {
            RouteValueDictionary routeValues = new RouteValueDictionary();
            routeValues.Add("Namespaces", "PaymentHelper.PaymentFlowTemplates.Default.Controllers");

            return new PaymentFlow.ModelTemplate.RouteModel()
            {
                RouteName = "DefaultTemplate.OrderInfo",
                Action = "OrderInfo",
                Controller = "DefaultTemplate",
                RoutePaymentInfo = routeValues
            };
        }

        public override RouteModel GetProcessResultRoute(string token)
        {
            RouteValueDictionary routeValues = new RouteValueDictionary();
            routeValues.Add("tokenPayment", token);

            return new PaymentFlow.ModelTemplate.RouteModel()
            {
                RouteName  = "DefaultTemplate.Complete",
                Action = "Complete",
                Controller = "DefaultTemplate",
                RoutePaymentInfo = routeValues
            };
        }
    }
}
